﻿<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="mtProducts.aspx.vb" Inherits="mtProducts" %>

<%@ Register Assembly="ISNet.WebUI.WebGrid" Namespace="ISNet.WebUI.WebGrid" TagPrefix="ISWebGrid" %>
<%@ Register Assembly="ISNet.WebUI.WebCombo" Namespace="ISNet.WebUI.WebCombo" TagPrefix="ISWebCombo" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <link href="js/magicsuggest-1.2.6.css" rel="stylesheet" />
    <script src="js/magicsuggest-1.2.6.js"></script>
    <script src="script/packed.js"></script>
    <script src="script/tinybox.js"></script>
    <link href="script/style.css" rel="stylesheet" />

    <script type="text/javascript" src="js/JSON.js"></script>
    <script>

        function OnRowSelect(controlId, tblName, rowIndex, rowElm) {
            var grid = ISGetObject("WebGrid1");
            var ar = grid.GetSelectedObject().GetRowElement();
            if (ar.IsAddNewRow) {
            }
            else {


                var combo = ISGetObject("ProductType2WebCombo");
                var Val2 = row.GetCells().GetNamedItem("ProductType").Value
                combo.SetValue(Val2);

                //var ar = grid.GetSelectedObject().GetRowElement();
            }
        }

        function WebGrid1_OnRowValidate(rowObject) {
            // retrieves WebGrid's object
            var grid = ISGetObject("WebGrid1");

            // access WebGrid's RootTable
            var rootTable = grid.RootTable;

            var row = rootTable.ToRowObject(rowObject); // convert the rowObject (HTMLElement) to WebGridRow object
            var htmlRow = row.GetElement();
            var cells = row.GetCells(); // get collection of WebGridCell
            var error = 0;
            var err_text = "กรุณาตรวจสอบ \n"

            //alert("1");
            var data
            data = wgGetCellByPosition(htmlRow, 1).innerText;
            if (data.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '').replace(/\s+/g, ' ') == "") {
                err_text = err_text + "\n   ● รหัสผลิตภัณฑ์                          ";
                error = error + 1;
            }

            data = wgGetCellByPosition(htmlRow,2).innerText;
            if (data.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '').replace(/\s+/g, ' ') == "") {
                err_text = err_text + "\n   ● ปีงบประมาณ                                   ";
                error = error + 1;
            }

            data = wgGetCellByPosition(htmlRow, 3).innerText;
            if (data.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '').replace(/\s+/g, ' ') == "") {
                err_text = err_text + "\n   ● ชื่อผลิตภัณฑ์                             ";
                error = error + 1;
            }

            data = wgGetCellByPosition(htmlRow, 4).innerText;
            if (data.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '').replace(/\s+/g, ' ') == "") {
                err_text = err_text + "\n   ● กลุ่มผลิตภัณฑ์                             ";
                error = error + 1;
            }

            //alert("2");
            if (error == 0) {
                return true;
            } else {
                alert(err_text);
                return false;
            }

        }

        function WebGrid1_OnEditKeyDown(controlId) {
            //alert(event.keyCode);
            var grid = ISGetObject("WebGrid1");

            var totalRows = grid.TotalRows;
            var totalCells = grid.TotalCells;
            var curRow = grid.GetSelectedObject().rowIndex;
            var curCell = grid.GetActiveEditCell().cellIndex;

            // if enter key
            if (event.keyCode == 13) {
                if (totalRows >= 2) {
                    if (curRow < totalRows - 1) {
                        keyDown(curRow, curRow + 1, curCell - 1);
                    }
                }

            }

                // if up arrow
            else if (event.keyCode == 38 && grid.GetSelectedObject().GetRowObject().Type != "NewRow") {
                if (totalRows >= 2) {
                    if (curRow > 0) {
                        keyDown(curRow, curRow - 1, curCell - 1);
                    }
                }
            }

                // if down arrow key
            else if (event.keyCode == 40 && grid.GetSelectedObject().GetRowObject().Type != "NewRow") {
                if (totalRows >= 2) {
                    if (curRow < totalRows - 1) {
                        keyDown(curRow, curRow + 1, curCell - 1);
                    }
                }
            }
                // if left arrow key
            else if (event.keyCode == 37) {

                if (curCell > 1) {

                    keyDown2(curRow, curCell - 2);

                }
            }
                // if right arrow key
            else if (event.keyCode == 39) {
                event.keyCode = 9;
                return true;
            }


        }

        function keyDown(curRow, nextRow, cell) {
            var grid = ISGetObject("WebGrid1");
            var nextRowEl = grid.RootTable.GetRow(nextRow);
            var curRowEl = grid.RootTable.GetRow(curRow);
            var cell1 = nextRowEl.GetCell(cell);
            var cell2 = curRowEl.GetCell(cell);
            grid.ExitEdit(1, 0, 0);
            grid.SetFocus();

            cell2.Select(true);

            nextRowEl.Select();

            cell1.ActivateEdit();
        }
        function keyDown2(curRow, cell) {
            var grid = ISGetObject("WebGrid1");

            var selectedObject = grid.GetSelectedObject(); // return object of type SelectedObject
            var selectedRow = selectedObject.ToRowObject();
            var cell1 = selectedRow.GetCell(cell);
            grid.SetFocus();
            cell1.Select(true);
            cell1.ActivateEdit();

        }

        function CustomRequestDel(del, code, year) {

            if (confirm('กรุณายืนยันการลบข้อมูล') == false) {
                return false
            }
            var grid = ISGetObject("WebGrid1");

            grid.AddInput("id", del);
            grid.AddInput("code", code);
            grid.AddInput("year", year)
            grid.AddInput("type", "1");
            grid.SendCustomRequest();

            event.cancelBubble = true;
            event.returnValue = false;
        }

        function closeJS() {
            var WebGrid1 = ISGetObject("WebGrid1");
            WebGrid1.Refresh();
        }

        function popuplist(id, idyear) {
            var grid = ISGetObject("WebGrid1");
            var row = grid.GetSelectedObject().GetRowObject();
            var key = row.KeyValue;

            //get the key value of selected row.
            TINY.box.show({ iframe: 'mtProductslPopup.aspx?PID=' + id + "&IDYear=" + idyear, boxid: 'frameless', width: 750, height: 500, fixed: false, maskid: 'lightmask', maskopacity: 40, closejs: function () { closeJS() } })
        }
        function popupProducts() {
            var pytxt = document.getElementById('<%=PlanYearsTxt.ClientID%>').value

            //get the key value of selected row.
            TINY.box.show({ iframe: 'mtProductslPopup.aspx?PlanYear=' + pytxt, boxid: 'frameless', width: 750, height: 500, fixed: false, maskid: 'lightmask', maskopacity: 40, closejs: function () { closeJS() } })
        }
        function CustomRequestChkDel() {
            if (confirm('กรุณายืนยันการลบข้อมูล') == false) {
                return false
            }
            var grid = ISGetObject("WebGrid1");
            var table = grid.GetRootTable();
            var checkedRows = table.GetCheckedRows();

            if (checkedRows.length == 0) {
                alert("Please check an item.");
                setTimeout(function () { grid.SetFocus(); }, 10);
            }
            else {

                grid.AddInput("type", "2");
                //alert("gfgfgfg");
                grid.SendCustomRequest();

            }


            event.cancelBubble = true;
            event.returnValue = false;
        }
    </script>
    <div style="padding: 10px 10px 10px 10px;">
        <div align="left" style="width: 97%; float: left; margin-top: 10px;">
            <asp:Label ID="ScreenName" runat="server"></asp:Label>
        </div>
        <div style="text-align: right; float: left; width: 97%; margin-top: 10px; -webkit-border-radius: 5px; padding: 5px 5px 5px 5px;">

            <div style="text-align: left; float: left; width: 65%; border: solid; border-color: gray; border-width: 1px; margin-top: 10px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; box-shadow: 5px 5px 5px #808080; padding: 5px 5px 5px 5px;">
                <table border="0">
                    <tr>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;    
                            <asp:Label ID="Label3" runat="server" Text="รหัสผลิตภัณฑ์"></asp:Label>
                            &nbsp;&nbsp;&nbsp;&nbsp;                   
                        </td>
                        <td>
                            <asp:TextBox ID="CodeTxt" runat="server" Width="100px" Style="text-align: left;" MaxLength="8"></asp:TextBox>
                        </td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;    
                            <asp:Label ID="Label1" runat="server" Text="ชื่อผลิตภัณฑ์"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;           
                              <asp:TextBox ID="SearchTxt" runat="server" Width="200px" Style="text-align: left;"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp; 
                                <asp:Label ID="Label4" runat="server" Text="กลุ่มผลิตภัณฑ์"></asp:Label>
                        </td>
                        <td colspan="2">
                            <ISWebCombo:WebCombo ID="ProductTypeGroupCombo" Width="150px" runat="server" UseDefaultStyle="True" Height="20px" DataSourceID="ProductTypeObjectDataSource" DataTextField="Description" DataValueField="Value">
                                <LayoutSettings AlwaysShowAllRowsOnDropdown="true" AlwaysShowHelpButton="false">
                                </LayoutSettings>
                            </ISWebCombo:WebCombo>
                            <asp:ObjectDataSource ID="ProductTypeObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByPRODUCT_TYPE" TypeName="MasterDataSetTableAdapters.AppConfigsTableAdapter"></asp:ObjectDataSource>
                        </td>
                    </tr>
                </table>
            </div>
            <div style="text-align: center; float: left; width: 32%; height: 52px; border: solid; border-color: gray; border-width: 1px; margin-top: 10px; margin-left: 10px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; box-shadow: 5px 5px 5px #808080; padding: 5px 5px 5px 5px;">
                &nbsp;&nbsp;&nbsp;&nbsp; 
                <asp:Label ID="Label2" runat="server" Text="ปีงบประมาณ"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp; 
                <asp:TextBox ID="PlanYearsTxt" runat="server" Width="40px" Style="text-align: center;" MaxLength="4"></asp:TextBox>

            </div>
        </div>
        <div style="text-align: right; float: left; width: 97%; border: solid; border-color: gray; border-width: 1px; margin-top: 10px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; box-shadow: 5px 5px 5px #808080; padding: 5px 5px 5px 5px;">

            <table id="t_countdata" runat="server" width="300" visible="false" border="0" style="float: left; margin-left: 13px;">
                <tr>
                    <td align="left" style="width: 80px;" valign="middle">
                        <asp:Label ID="L_data" runat="server" Text="ผลการค้นหา: "></asp:Label>
                    </td>
                    <td align="left">
                        <asp:Label ID="L_CountData" runat="server" ForeColor="red"></asp:Label>
                        <asp:Label ID="L_test" runat="server" CssClass="label" Visible="false"></asp:Label>
                        &nbsp;
                    </td>
                </tr>
            </table>
            <table style="float: right;">
                <tr>
                    <td>
                        <asp:ImageButton ID="SearhBtn" runat="server" ImageUrl="Icons/search.jpg" />
                        <asp:ImageButton ID="delete_btn" runat="server" ImageUrl="Icons/delete.jpg" OnClientClick="CustomRequestChkDel(); return false" />
                        &nbsp;<asp:ImageButton ID="SaveBtn" runat="server" ImageUrl="Icons/save.jpg" Style="height: 21px" />
                    </td>
                </tr>
            </table>
        </div>

        <div style="float: left; width: 97%; border: solid; border-color: gray; border-width: 1px; margin-top: 10px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; box-shadow: 5px 5px 5px #808080; padding: 5px 5px 5px 5px;">
            <ISWebGrid:WebGrid ID="WebGrid1" runat="server" UseDefaultStyle="True" Width="100%" DefaultStyleMode="Silver" Height="390px" OnAddRow="WebGrid1_AddRow" OnUpdateRow="WebGrid1_UpdateRow" OnInitializePostBack="WebGrid1_InitializePostBack" OnInitializeRow="WebGrid1_InitializeRow">
                <LayoutSettings AllowAddNew="Yes" EditOnClick="True" AllowColumnMove="No"
                    AllowEdit="Yes" AllowDelete="Yes" AllowSorting="Yes" ColumnFooters="Yes" NewRowLostFocusAction="AlwaysUpdate"
                    ResetNewRowValuesOnError="False" AlwaysShowHelpButton="false" PagingMode="ClassicPaging" PagingSize="12"
                    PagingLoadMode="Automatic">
                    <RowStyle Font-Names="Tahoma" Font-Size="9" CustomRules="overflow: hidden;" />
                    <AlternatingRowStyle Font-Names="Tahoma" Font-Size="9" BackColor="#fbfbfb" CustomRules="overflow: hidden;" />
                    <ClientSideEvents OnRowValidate="WebGrid1_OnRowValidate" OnRowContextMenu="WebGrid1_OnRowContextMenu" OnEditKeyDown="WebGrid1_OnEditKeyDown" />
                </LayoutSettings>
                <RootTable DataKeyField="Id" NewRowInfoText="เพิ่มรายการ">
                    <Columns>
                        <ISWebGrid:WebGridColumn IsRowChecker="true" Name="checker">
                        </ISWebGrid:WebGridColumn>
                        <ISWebGrid:WebGridColumn Caption="รหัสผลิตภัณฑ์" DataMember="ProductCode" Name="ProductCode" Width="100" TextboxMaxlength="8">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Center" />
                        </ISWebGrid:WebGridColumn>
                        <ISWebGrid:WebGridColumn Caption="ปีงบประมาณ" DataMember="PlanYear" DataType="System.Int16" Name="PlanYear" Width="100" TextboxMaxlength="4">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Center" />
                        </ISWebGrid:WebGridColumn>
                        <ISWebGrid:WebGridColumn Caption="ชื่อผลิตภัณฑ์" DataMember="Description" Name="Description" Width="300">
                            <HeaderStyle HorizontalAlign="Center" />
                        </ISWebGrid:WebGridColumn>
                        <ISWebGrid:WebGridColumn Caption="กลุ่มผลิตภัณฑ์" Name="ProductType" DataMember="ProductType" Width="150px" EditType="WebComboNET" WebComboID="ProductType2WebCombo">
                            <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Left" />
                            <ValueList DataTextField="Description" DataSourceID="ProductType2ObjectDataSource"
                                DataValueField="Value">
                            </ValueList>
                        </ISWebGrid:WebGridColumn>
                        <ISWebGrid:WebGridColumn Caption="รายละเอียด" ColumnType="Template" Name="Attachment" Width="70">
                            <CellTemplate>
                                <a href="#" style="text-decoration: none; border: none;">
                                    <img src="Icons/document_info.png" height="16px" style="text-decoration: none; border: none;" title="รายละเอียด" onclick="popuplist(<%# Eval("Id")%>,<%# Eval("PlanYear")%>);" /></a>

                                <a href="#" style="text-decoration: none; border: none;">
                                    <img src="Icons/ic_deleteRed.png" height="16px" id='<%# Eval("Id")%>' style="text-decoration: none; border: none;" title="ลบ" onclick="CustomRequestDel('<%# Eval("Id")%>','<%# Eval("ProductCode")%>','<%# Eval("PlanYear")%>');" />
                                </a>
                            </CellTemplate>
                             <HeaderStyle HorizontalAlign="Center" />
                            <CellStyle HorizontalAlign="Center"></CellStyle>
                        </ISWebGrid:WebGridColumn>

                    </Columns>
                </RootTable>
            </ISWebGrid:WebGrid>
            <ISWebCombo:WebCombo ID="ProductType2WebCombo" Width="150px" runat="server" UseDefaultStyle="True" Height="20px" DataSourceID="ProductTypeObjectDataSource" DataTextField="Description" DataValueField="Value">
                <LayoutSettings AlwaysShowAllRowsOnDropdown="true" AlwaysShowHelpButton="false">
                </LayoutSettings>
            </ISWebCombo:WebCombo>
            <asp:ObjectDataSource ID="ProductType2ObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByPRODUCT_TYPE" TypeName="MasterDataSetTableAdapters.AppConfigsTableAdapter"></asp:ObjectDataSource>
        </div>
    </div>
</asp:Content>

